package a51qindian.com.comprehensive.activity;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.li.interfaces.OnItemClickListener;
import com.li.utils.ToastUtil;
import com.li.widget.MultipleLayout;
import com.li.widget.TopNavigationBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.adapter.ShopAdvAdapter;
import a51qindian.com.comprehensive.base.QDBaseActivity;
import a51qindian.com.comprehensive.items.LinearItemDecoration;
import a51qindian.com.comprehensive.model.BaseModel;
import a51qindian.com.comprehensive.model.ShopAdvInfo;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import a51qindian.com.comprehensive.net.SelectorObserver;
import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public class ShopAdvListActivity extends QDBaseActivity implements OnItemClickListener {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.recycler_adv)
    RecyclerView mRecyclerAdv;
    @BindView(R.id.layout_content)
    MultipleLayout mLayoutContent;


    private ShopAdvAdapter mShopAdvAdapter;
    private List<ShopAdvInfo> mShopAdvInfoList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shop_adv_list;
    }

    @Override
    protected void initSth() {
        super.initSth();

        mTopNavigation.setNavigationBarListener(this);
        mShopAdvAdapter = new ShopAdvAdapter(this);
        mRecyclerAdv.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerAdv.addItemDecoration(new LinearItemDecoration());
        mRecyclerAdv.setAdapter(mShopAdvAdapter);
        mShopAdvInfoList = new ArrayList<>();
        mShopAdvAdapter.setItemClickListener(this);
        showShopAdvList(mUid);
    }

    private void showShopAdvList(String uid) {
        mLayoutContent.onLoadingWithContent();
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        ObservableHelper.createObservable(RetrofitCreator.createService().shopAdvList(map))
                .subscribe(new SelectorObserver<BaseModel<List<ShopAdvInfo>>>() {
                    @Override
                    protected void onErrorCodeInfo(int code, String info) {
                        mLayoutContent.onSuccess();
                        ToastUtil.showToast(ShopAdvListActivity.this, info);
                    }

                    @Override
                    protected void onSuccess(BaseModel<List<ShopAdvInfo>> listBaseModel) {
                        mLayoutContent.onSuccess();
                        mShopAdvInfoList = listBaseModel.getData();
                        mShopAdvAdapter.setShopAdvInfoList(mShopAdvInfoList);
                    }

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onSuccess();
                        ToastUtil.showToast(ShopAdvListActivity.this, e.getMessage());
                    }
                });
    }

    @Override
    public void onItemClick(View view, final int pos) {
        AlertDialog.Builder builder = new AlertDialog.Builder(ShopAdvListActivity.this)
                .setTitle("提示")
                .setMessage("是否将" + mShopAdvInfoList.get(pos).getName() + "置顶")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        applyAdv(mUid, mShopAdvInfoList.get(pos).getArea_id(), pos);
                    }
                });
        builder.create().show();
    }

    /**
     * 将广告置顶
     *
     * @param uid    用户编号
     * @param areaId 广告编号
     * @param pos    位置 用来更新界面
     */
    private void applyAdv(String uid, String areaId, final int pos) {
        mLayoutContent.onLoadingWithContent("正在申请");
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("area_id", areaId);
        ObservableHelper.createObservable(RetrofitCreator.createService().shopAdvApply(map))
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        mLayoutContent.onSuccess();
                        try {
                            JSONObject object = new JSONObject(s);
                            int code = object.getInt("code");
                            if (code != 1) {
                                ToastUtil.showToast(ShopAdvListActivity.this,object.getString("info"));
                                return;
                            }
//                            mShopAdvInfoList.remove(pos);
//                            mShopAdvAdapter.notifyItemRemoved(pos);
                            ToastUtil.showToast(ShopAdvListActivity.this,object.getString("data"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onSuccess();
                        ToastUtil.showToast(ShopAdvListActivity.this, e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
